package solution104;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 104. Maximum Depth of Binary Tree
 * 二叉树的最大深度
 * 相关：110. Balanced Binary Tree
 * 判断二叉树是否是平衡二叉树
 */
public class Solution {
    // 深度优先搜索DFS
    public int maxDepth(TreeNode root) {
        if (root == null) return 0;
        return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
    }

    public int maxDepthRecursive(TreeNode root) {
        if (root == null) return 0;
        int res = 0;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (queue.size()!=0) {
            res++;
            int n = queue.size();
            for (int i = 0; i < n; i++) {
                TreeNode t = queue.poll();
                if (t.left!=null) queue.add(t.left);
                if(t.right!=null) queue.add(t.right);
            }
        }
        return res;
    }


}
